package com.weaver.seconddev.custom.hook;

import cn.hutool.core.codec.Base64;
import com.weaver.ebuilder.datasource.api.entity.ExecuteSqlEntity;
import com.weaver.ebuilder.datasource.api.enums.SourceType;
import com.weaver.ebuilder.datasource.api.service.DataSetService;
import com.weaver.loom.api.annotation.RegHook;
import com.weaver.loom.context.register.AbstractRegHookEvent;
import com.weaver.verupgrade.general.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 判断流程是否允许自动审批
 *
 * @Author GJC
 * @Date 2024/12/03 18:24
 */
@Slf4j
@RegHook(module = "workflow", event = "wfcAutoApproveAllowTargetNode", order = 0)
public class TargetNodeIdAllowAutoApproveHook extends AbstractRegHookEvent {
    
    @Autowired
    private DataSetService dataSetService;
    
    
    
    @Override
    public Object handle(Object[] args) throws Exception {
        //请求参数
        long requestId = (long) args[0];
        //目标节点id
        long nodeId = (long) args[1];
        //1：后续节点自动处理，2：相邻节点自动处理
        String source = Util.null2String(args[1]);
        //目标用户id
        long userId = (long) args[1];
        //目标流程id
        long workflowId = (long) args[1];
        boolean judge = true;
        
        String sql = "select * from uf_zdclpz where lcid ='"+workflowId+"' and jdid ='"+nodeId+"'  and sfqy = 1";
        ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity();
        executeSqlEntity.setSql(Base64.encode(sql));
        //groupid,可以访问  E10地址/api/datasource/ds/group?sourceType=LOGIC 获取
        executeSqlEntity.setGroupId("weaver-ebuilder-form-service");
        executeSqlEntity.setSourceType(SourceType.LOGIC);
        Map<String, Object> datas = dataSetService.executeSql(executeSqlEntity);
        log.error("=====TargetNodeIdAllowAutoApproveHook判断流程是否允许自动审批===datas{}",datas);
        //返回值的records是一个map集合
        String jdid_val = "";
        List<LinkedHashMap<String, Object>> relationConfigList = (List<LinkedHashMap<String, Object>>)datas.get("records");
        log.error("=====TargetNodeIdAllowAutoApproveHook判断流程是否允许自动审批===relationConfigList{}",relationConfigList);
        if(null!=relationConfigList) {
            for (int i = 0; i < relationConfigList.size(); i++) {
                LinkedHashMap<String, Object> records = relationConfigList.get(i);
                jdid_val = Util.null2String(records.get("jdid"));
                judge = false;
            }
        }
        
        //
        if(!jdid_val.equals("")) {
            sql = "select * from uf_zdclpz where lcid ='" + workflowId + "' and (jdid is null or jdid = '') and sfqy = 1";
            executeSqlEntity = new ExecuteSqlEntity();
            executeSqlEntity.setSql(Base64.encode(sql));
            //groupid,可以访问  E10地址/api/datasource/ds/group?sourceType=LOGIC 获取
            executeSqlEntity.setGroupId("weaver-ebuilder-form-service");
            executeSqlEntity.setSourceType(SourceType.LOGIC);
            datas = dataSetService.executeSql(executeSqlEntity);
            log.error("=====TargetNodeIdAllowAutoApproveHook判断流程是否允许自动审批===datas{}", datas);
            //返回值的records是一个map集合
            String lcid_val = "";
            List<LinkedHashMap<String, Object>> relationConfigList2 = (List<LinkedHashMap<String, Object>>) datas.get("records");
            log.error("=====TargetNodeIdAllowAutoApproveHook判断流程是否允许自动审批===relationConfigList2{}", relationConfigList2);
            if (null != relationConfigList2) {
                for (int i = 0; i < relationConfigList2.size(); i++) {
                    LinkedHashMap<String, Object> records = relationConfigList2.get(i);
                    lcid_val = Util.null2String(records.get("lcid"));
                    judge = false;
                }
            }
        }
        log.error("=====TargetNodeIdAllowAutoApproveHook判断流程是否允许自动审批===judge{}", judge);
        if(!judge){
            return true;
        }
        return true;
    }
    
    
    
    public boolean ishasedititem(String workflowid ,String nodeid) {
        boolean hasEditItem = true;
        return hasEditItem;
    }
    
    
    
    
    
}